﻿using System.Diagnostics;
using System;

namespace blue.connection.mag_guide
{
    class TestMagneticGuideCon
    {
        MagneticGuide mag_;
        private Stopwatch sw_;
        private long[] tc_arr_;
        private int cnt_all_ = 0;
        private int cnt_success_ = 0;
        private int cnt_fail_ = 0;
        private long tc_max_ = 0;   // consume time
        private long tc_min_ = 9999;
        private long tc_all_ = 0;
        private long tc_first_ = 0;
        private long tcc_10_ = 0;
        private long tcc_30_ = 0;
        private long tcc_100_ = 0;

        public void test(string portname,int test_cnt, int period) {
            mag_ = new MagneticGuide(portname);
            tc_arr_ = new long[test_cnt];
            sw_ = Stopwatch.StartNew();

            while (test_cnt-- > 0) {
                showMsg("start read -----------------" + test_cnt);
                sw_.Restart(); // Begin timing

                if (mag_.updateSensorDataRT())
                {
                    byte[] data = mag_.getRawData();
                    double pos_f1 = mag_.getCenter(SensorID.FORWARD1);
                    double pos_f2 = mag_.getCenter(SensorID.FORWARD2);
                    double pos_b1 = mag_.getCenter(SensorID.BEHIND1);
                    double pos_b2 = mag_.getCenter(SensorID.BEHIND2);
                    double pos_l = mag_.getCenter(SensorID.LEFT);
                    double pos_r = mag_.getCenter(SensorID.RIGHT);
                    // Stop timing
                    sw_.Stop();  // Success
                    cnt_success_++;
                    System.Console.WriteLine("{F1,F2,B1,B2,L,R} = {0} ,{1} ,{2} "+
                                            ",{3} ,{4} ,{5}, {6}",
                                            pos_f1,pos_f2,pos_b1,pos_b2,pos_l,pos_r);
                    showMsg("success read " + BitConverter.ToString(data, 0));
                }
                else
                {
                    sw_.Stop();
                    cnt_fail_++;
                    showMsg("failed to update data");
                }

                updateLog();
                System.Threading.Thread.Sleep(period);
            }


        }

        public void showLog()
        {
            string count_log = string.Format("Count (All) = {0},"+
                " Count (Success) = {1}, Count (Fail) = {2}",cnt_all_,cnt_success_,cnt_fail_);
            string tc_log = string.Format("Cosume Time >> all={0}ms"+
                "  max={1}ms  min={2}ms  avg={3}ms  first={4}"+
                "  tc(10,30)={5} tc(31~100)={6} tc(100+)={7}"
                ,tc_all_, tc_max_, tc_min_, tc_all_/cnt_all_
                , tc_first_, tcc_10_, tcc_30_, tcc_100_);

            System.Console.WriteLine(count_log);
            System.Console.WriteLine(tc_log);
            System.Console.WriteLine(mag_.getLog());
        }

        private void updateLog() {
            long cur_ct = sw_.ElapsedMilliseconds;
            if (cur_ct > tc_max_) tc_max_ = cur_ct;

            if (cur_ct < tc_min_) tc_min_ = cur_ct;

            if (tc_all_ == 0) tc_first_ = cur_ct;

            if (cur_ct > 100) tcc_100_++;
            else if (cur_ct > 30) tcc_30_++;
            else if (cur_ct > 10) tcc_10_++;

            tc_all_ += cur_ct;
            tc_arr_[cnt_all_] = cur_ct;
            cnt_all_++;
        }

        private void showMsg(string msg) {
            string time = System.DateTime.Now.ToString("hh:mm:ss.fff");
            time += " > ";
            time += msg;
            System.Console.WriteLine(time);
        }
    }
}
